from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from .models import Area
from IHome.settings.dev import logger


class AreaView(View):

    def get(self, request):

        try:
            address_list = Area.objects.all()
        except Exception as e:
            logger.error(e)
            return JsonResponse({'errmsg': 'DBERR', 'errno': '4001'})
        else:
            data_list = []
            for addr in address_list:
                dic = {
                    'aid': addr.pk,
                    'aname': addr.name
                }
                data_list.append(dic)

        return JsonResponse({'errmsg': 'DBERR', 'errno': '0', 'data': data_list})